﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MINERP.COM;
using Microsoft.Practices.EnterpriseLibrary.Data;
using System.Data.Common;
using System.Data;

namespace MINERP.DAL.DisJoin
{
    public  class DisJoinDAL:DALBase
    {
        public List<Shop> GetAllShop()
        {
            return DataEntites.Shop.ToList();
        }
        public List<Tb_OrderBag> GetAllBag(int inShopId,DateTime inStartTime, DateTime inEndTime)
        {
            int ordeBagStatus = (int)OrderBagStatus.Doing;
            int orderBagStatus_Complete = (int)OrderBagStatus.Completed;
            if (inStartTime != DateTime.MinValue && inEndTime != DateTime.MinValue)
            {
                if (inShopId != 0)
                {
                    return DataEntites.Tb_OrderBag.Where(p => p.Shop.ShopId == inShopId && p.OrderTime > inStartTime && p.OrderTime < inEndTime && p.Status != orderBagStatus_Complete).ToList();
                }
                else
                {
                    return DataEntites.Tb_OrderBag.Where(p => p.OrderTime > inStartTime && p.OrderTime < inEndTime && p.Status != orderBagStatus_Complete).ToList();

                }
            }
            else
            {
                return DataEntites.Tb_OrderBag.Where(p => p.Shop.ShopId == inShopId && p.Status != orderBagStatus_Complete).ToList();
            }
        }
        /// <summary>
        /// 取得格子名称
        /// </summary>
        /// <param name="inProductId"></param>
        /// <returns></returns>
        public string GetGridName(int inProductId)
        {


           Grid grid= DataEntites.Grid.Where(p=> p.SomeId == inProductId&&p.GridSubArea.SubAreaID==5).FirstOrDefault();
            if(grid==null)
                return null;
            else
            {
                return grid.GridName;
            }
        }
        public string GetOutGridName(int inProductId)
        {
            Database db = null;
            string GridName  ="";
            try
            {
                db = DatabaseFactory.CreateDatabase();
                string strSQL = "select GridName from dbo.Grid,dbo.OutProcessRecord,GridSubArea  " +
                                 "where Grid.SomeId=OutProcessRecord.OutShopId  and Grid.status=1 and GridSubArea.SubAreaID=Grid.SubAreaID    " +
                                 "and OutProcessRecord.OrderProductId=" + inProductId+
                                 " and GridSubArea.Type=" + (int)SubAreaType.OutShop;




                DbCommand dbCommand = db.GetSqlStringCommand(strSQL);

                using (IDataReader dr = db.ExecuteReader(dbCommand))
                {
                    while (dr.Read())
                    {
                       

                            GridName = (dr.GetString(0));
                       


                    }
                }
            }
            catch (Exception ex)
            {
                throw;
            }
            return GridName;
        }
        /// <summary>
        /// 转出产品
        /// </summary>
        /// <returns></returns>
        public string GetZhuanString(string inBarCode)
        {
            orderInfo oi = DataEntites.orderInfo.Where(p=>p.OrderProductCode==inBarCode).FirstOrDefault();
            if (oi == null)
            {
                return null;
            }
            else
            {
                string mark=oi.Remark;
                int index = mark.IndexOf("转");
                int index2 = mark.IndexOf("留");
                if (index == -1)
                {
                    if (index2 == -1)
                    {

                        return null;
                    }
                    else
                    {
                        return mark.Substring(index2);
                    }
                }
                else
                    return mark.Substring(index);
            }
        }
        public int OutCount(int inOutShopId)
        {
            return DataEntites.OutProcessRecord.Where(op => (op.Status == (int)OutProcessRecordStatus.InGrid || op.Status == (int)OutProcessRecordStatus.OutWaitFenDui) && op.OutProcessShop.OutShopId == inOutShopId).Count();
        }
        public int OutInGridCount(int inOutShopId)
        {
            return DataEntites.OutProcessRecord.Where(op => (op.Status == (int)OutProcessRecordStatus.InGrid ) && op.OutProcessShop.OutShopId == inOutShopId).Count();
        }
        public string GetOutShopEamil(int inOutShopId)
        {
            Database db = null;
            string Email = "";
            try
            {
                db = DatabaseFactory.CreateDatabase();
                string strSQL = "select EmailAddress from dbo.OutProcessShop  where OutShopId=" + inOutShopId;




                DbCommand dbCommand = db.GetSqlStringCommand(strSQL);

                using (IDataReader dr = db.ExecuteReader(dbCommand))
                {
                    while (dr.Read())
                    {


                        Email = (dr.GetString(0));



                    }
                }
            }
            catch (Exception ex)
            {
                throw;
            }
            return Email;
        }
    }
}
